package org.rcloud.io.service.impl;

import org.rcloud.io.Utils.ImageUtils;
import org.rcloud.io.Utils.ResultVo;
import org.rcloud.io.Utils.UploadVo;
import org.rcloud.io.mapper.*;
import org.rcloud.io.model.*;
import org.rcloud.io.service.ShootUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;

@Service
public class ShootUploadServiceImpl implements ShootUploadService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ArticleCategoryMappingMapper articleCategoryMappingMapper;

    @Autowired
    private ShootImageMapper shootImageMapper;

    @Autowired
    private UserArticleMappingMapper userArticleMappingMapper;

    @Autowired
    private UserMapper userMapper;

    @Value("${file.uploadFolder}")
    private String uploadFolder;
    @Override
    public ResultVo upload(UploadVo uploadVo) {

        //获取原图路径
        String path = uploadVo.getPath();
        File file1 = new File(path);
        System.out.println(path);
        //切割获取图片名称
        String[] split = path.split("/");
        String name = split[split.length - 1];

        String s = uploadFolder +"image/" + name;
        File file = new File(s);

        String s1 = uploadFolder +"images/" + name;
        File file3 = new File(s1);

        File file2 = new File("/var/lib/docker/tomcat/apache-tomcat-8.5.37/webapps/wechat-api-0.0.1-SNAPSHOT/WEB-INF/classes/static/upload/images/1559290943685.jpg");
        if (!file.exists()) {
            file.getParentFile().mkdirs();
            try {
                ImageUtils.createThumbnail(file3,file,750,750);
                ImageUtils.makeWatermark(file,file2,10,10,0.6F);
            } catch (IOException e) {
                e.printStackTrace();
                return ResultVo.setError(e.getMessage());
            }
        }

        Integer uId = null;
        if (uploadVo.getuId() == null || uploadVo.getuId().equals("")) {
            uId = uploadVo.getId();
        } else {

            User user = userMapper.selectByKey(uploadVo.getuId());
            uId = user.getId();
        }

        int i = articleMapper.selectCount(uploadVo.getgId(), uId);
        if (i < 5) {

            //添加作品文章
            Article article = new Article();
            article.setTitle(uploadVo.getTitle());
            article.setThumbnail(name);
            article.setContent(uploadVo.getContent());
            articleMapper.insert(article);

            //原图保存
            ShootImage shootImage = new ShootImage();
            shootImage.setSite(uploadVo.getSite());
            shootImage.settId(article.getId());
            shootImage.setUrl("https://wdsx.fywljt.cn/wechat-api-0.0.1-SNAPSHOT/upload/image/"+ name);
            shootImageMapper.insert(shootImage);

            //保存赛事和文章的关联
            ArticleCategoryMappingKey key = new ArticleCategoryMappingKey();
            key.setArticleId(article.getId());
            key.setCategoryId(uploadVo.getgId());
            articleCategoryMappingMapper.insert(key);

            //保存文章和用户的关联
            UserArticleMapping mapping = new UserArticleMapping();
            if (uploadVo.getuId() == null || uploadVo.getuId().equals("")) {
                mapping.setuId(uploadVo.getId());
                mapping.setaId(article.getId());
                userArticleMappingMapper.insert(mapping);
            } else {

                User user = userMapper.selectByKey(uploadVo.getuId());
                mapping.setaId(article.getId());
                mapping.setuId(user.getId());
                userArticleMappingMapper.insert(mapping);
            }

            return ResultVo.setOK("添加成功");
        } else {
            return ResultVo.setError("作品添加已达上限，请在其他栏目上传");
        }

    }
}
